package com.zsix.system.strategy.processVariables;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zsix.common.dto.controllerParams.ProceParams;
import com.zsix.common.entity.taskAssignee.GenerWork;
import com.zsix.common.util.HttpsUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Hashtable;

@Component
public class WebAPIParams implements ProcessVariableStrategy {
    /**
     * 参数赋值
     * @param generWork
     * @param proceParams
     * @return
     */
    @Override
    public HashMap<String,Object> SetParams(GenerWork generWork,
             ProceParams proceParams, HashMap<String,Object> hashMap,String processKey,String task_def_key
            ,String BusinessKey){

        JSONObject dataJson= JSON.parseObject(generWork.getData_json());
        //增加header参数
        java.util.Map<String, String> headerMap = new Hashtable<String, String>();
        headerMap.put("Content-Type", "application/json");
        headerMap.put("charset", "utf-8");

        //请求参数
        HashMap<String,Object> objectHashMap=new HashMap<>();
        //业务数据主键
        objectHashMap.put("businessKey",BusinessKey);
        //流程运行实例id
        objectHashMap.put("proce_inst_id",generWork.getProce_inst_id());
        //流程key
        objectHashMap.put("processKey",processKey);
        //节点key
        objectHashMap.put("task_def_key",task_def_key);
        //业务表数据
        objectHashMap.put("others",dataJson);

        try {
            //请求返回信息json
            JSONObject json = null;
            String jsonData = HttpsUtils.post(proceParams.getParamValue(), headerMap, objectHashMap.toString());
            if(proceParams.getValueType().equals("2"))//布尔型
                hashMap.put(proceParams.getParamKey(), Boolean.valueOf(jsonData));
            else if(proceParams.getValueType().equals("1"))//数值型
                hashMap.put(proceParams.getParamKey(), Float.valueOf(jsonData));
                //json对象
            else if(proceParams.getValueType().equals("3")){
                json = JSONObject.parseObject(jsonData);
                hashMap.put(proceParams.getParamKey(), json);
            }
            else//字符型
                hashMap.put(proceParams.getParamKey(), jsonData);
        }
        catch (Exception ex){
            hashMap.put(proceParams.getParamKey(), ex.getMessage());
        }
        return hashMap;
    }
    @Override
    public String getType(){
        return "3";
    }
}
